package com.example.springmvc_adv.controller;

import com.example.springmvc_adv.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ExceptionAdvice
 * @Description TODO
 * @Author H.
 * @Date 2023/5/4 19:04
 * @Version 1.0
 */
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Object handle(Exception e){
        Map<String,Object> map = new HashMap<>();
        map.put("code",500);
        map.put("message",e.getMessage());
        return map;
    }
}
